package com.huluobo.a2111douyindemo;

import static android.Manifest.permission.CAMERA;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.RECORD_AUDIO;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;
import com.huluobo.a2111douyindemo.adapter.CommonPagerAdapter;
import com.huluobo.a2111douyindemo.databinding.ActivityMainBinding;
import com.huluobo.a2111douyindemo.home.HomeFragment;
import com.huluobo.a2111douyindemo.message.MessageFragment;
import com.huluobo.a2111douyindemo.mine.MineFragment;
import com.huluobo.a2111douyindemo.mine.WorksFragment;
import com.huluobo.a2111douyindemo.startLive.StartLiveActivity;
import com.huluobo.a2111douyindemo.video.VideoFragment;
import com.huluobo.mvvm_base.mvvm.base.view.BaseActivity;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends BaseActivity<ActivityMainBinding> {

    //vp2的adapter
    private CommonPagerAdapter pagerAdapter;

    //fragment的数据源
    private List<Fragment> fragmentList = new ArrayList<>();

    @Override
    protected void initData() {
        fragmentList.clear();
        fragmentList.add(new HomeFragment());
        fragmentList.add(new VideoFragment());
        fragmentList.add(new MessageFragment());
        fragmentList.add(new MineFragment());

        pagerAdapter = new CommonPagerAdapter(this, fragmentList);
        dataBinding.mainVp2.setAdapter(pagerAdapter);
    }

    @Override
    protected void initView() {
        //声明动态权限
        requestPermissions(new String[]{RECORD_AUDIO, CAMERA, READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE}, 101);

        //vp2翻页时需要控制底部导航栏选中效果
        dataBinding.mainVp2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position) {
                    case 0:
                        dataBinding.mainNav.getMenu().getItem(0).setChecked(true);
                        break;
                    case 1:
                        dataBinding.mainNav.getMenu().getItem(1).setChecked(true);
                        break;
                    case 2:
                        dataBinding.mainNav.getMenu().getItem(3).setChecked(true);
                        break;
                    case 3:
                        dataBinding.mainNav.getMenu().getItem(4).setChecked(true);
                        break;
                }
            }
        });

        //底部导航切换时,vp2需要翻页
        dataBinding.mainNav.setOnItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.main_tab_home:
                    dataBinding.mainVp2.setCurrentItem(0);
                    break;
                case R.id.main_tab_video:
                    dataBinding.mainVp2.setCurrentItem(1);
                    break;
                case R.id.main_tab_start:
                    Intent intent = new Intent(this, StartLiveActivity.class);
                    startActivity(intent);
                    break;
                case R.id.main_tab_message:
                    dataBinding.mainVp2.setCurrentItem(2);
                    break;
                case R.id.main_tab_mine:
                    dataBinding.mainVp2.setCurrentItem(3);
                    break;
            }

            return true;
        });

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }
}